package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherSeivice;

import java.util.Scanner;

public class TeacherController {

    private  Scanner sc = new Scanner(System.in);
    private TeacherSeivice teacherSeivice = new TeacherSeivice();

    public void start() {
        teacherLoop:
        while (true) {
            System.out.println("----------欢迎来到学生管理系统---------");
            System.out.println("1、增加老师 2、删除老师 3、修改老师 4、查找老师 5、退出");
            System.out.println("请输入您的选择：");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTteacher();
                    break;
                case "2":
                    deleteTeacherById();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出，感谢您的使用！");
                    break teacherLoop;
                default:
                    System.out.println("您输入的信息有误，请检查重新输入！");
                    break;
            }
        }
    }

    public void updateTeacher() {

        String id = inputTeacherId();

        Teacher newTeacher = inputTeacherInfo(id);


        TeacherSeivice.updateTeacher(id,newTeacher);

        System.out.println("修改成功！");
    }

    public void deleteTeacherById() {

        String id = inputTeacherId();
        TeacherSeivice.deleteTeacherById(id);

        System.out.println("删除成功！");
    }

    public void findAllTeacher() {
        Teacher[] teachers = TeacherSeivice.findAllTeacher();

        if(teachers == null){
            System.out.println("查无信息，请检查并且重新输入！");
            return;
        }

        System.out.println("学号\t\t\t姓名\t\t年龄\t\t\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if(t != null){
                System.out.println(t.getId() + "\t" + t.getName() + "\t\t" +
                        t.getAge() + "\t\t" +t.getBirthday());
            }
        }
    }

    public void addTteacher() {
        String id;
        while(true){
            System.out.println("请输入老师id：");
            id = sc.next();
            boolean exists = TeacherSeivice.isExists(id);

            if(exists){
                System.out.println("id已经被占用，请重新输入：");
            }else{
                break;
            }

            Teacher t = inputTeacherInfo(id);

            boolean result = TeacherSeivice.addTeacher(t);

            if(result){
                System.out.println("添加成功！");
            }else {
                System.out.println("添加失败！");
            }
        }
    }

    public  String inputTeacherId(){
        String id;
        while(true){
            System.out.println("请输入id：");
            id = sc.next();
            boolean exists = TeacherSeivice.isExists(id);
            if(!exists){
                System.out.println("您输入的id不存在，请重新输入：");
            }else {
                break;
            }
        }

        return id;
    }

    public  Teacher inputTeacherInfo(String id){
        System.out.println("请输入老师姓名：");
        String name = sc.next();
        System.out.println("请输入老师年龄：");
        String age = sc.next();
        System.out.println("请输入老师生日：");
        String birthday = sc.next();

        Teacher t = new Teacher();
        t.setId(id);
        t.setName(name);
        t.setAge(age);
        t.setBirthday(birthday);

        return t;
    }
}
